package com.yupi.picturebackend.utils;

/**
 * 颜色转换类
 */
public class ColorTransformUtils {
    private ColorTransformUtils(){

    }

    public static String getStandardColor(String color){
        // 去除可能存在的0x前缀
        String input = color.startsWith("0x")?color.substring(2):color;
        int length = input.length();
        // 长度为3，直接返回
        if (length == 3){
            return "0x000000" ;
        }
        int index = 0;
        StringBuilder expanded = new StringBuilder();
        // 处理三个颜色分量
        for (int i = 0; i < 3; i++) {
            char current = input.charAt(index);
            if (current == '0'){
                expanded.append("00");
                index++;
            }else {
                // 正常分量处理
                if (index + 1 < length){
                    expanded.append(current).append(input.charAt(index + 1));
                    index += 2;
                }else {
                    // 处理最后一个分量
                    expanded.append(current).append('0');
                    index += 2;
                }
                }
            }
        return "0x" + expanded.toString();

        //        if (color.length() == 7){
//            color = color.substring(0,4) + "0" + color.substring(4,7);
//        }
    }

    public static void main(String[] args) {
        // 测试用例
        System.out.println(getStandardColor("000"));     // 0x000000
        System.out.println(getStandardColor("0a00"));    // 0x00a000
        System.out.println(getStandardColor("a0b40"));   // 0xa0b400
        System.out.println(getStandardColor("0ab0"));    // 0x00ab00
        System.out.println(getStandardColor("00ab"));   // 0x0000ab
        System.out.println(getStandardColor("0ab00"));  // 0x00ab00
    }
}
